package com.pig4cloud.pig.farm.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.farm.entity.FarmProductEntity;
import com.pig4cloud.pig.farm.service.FarmProductService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 农业种植产品
 *
 * @author lty
 * @date 2024-11-04 21:25:12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/farmProduct" )
@Tag(description = "farmProduct" , name = "农业种植产品管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class FarmProductController {

    private final  FarmProductService farmProductService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param farmProduct 农业种植产品
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('farm_farmProduct_view')" )
    public R getFarmProductPage(@ParameterObject Page page, @ParameterObject FarmProductEntity farmProduct) {
        LambdaQueryWrapper<FarmProductEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(farmProduct.getProductName()),FarmProductEntity::getProductName,farmProduct.getProductName());
		wrapper.eq(StrUtil.isNotBlank(farmProduct.getProductCode()),FarmProductEntity::getProductCode,farmProduct.getProductCode());
        return R.ok(farmProductService.page(page, wrapper));
    }


    /**
     * 通过id查询农业种植产品
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('farm_farmProduct_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(farmProductService.getById(id));
    }

    /**
     * 新增农业种植产品
     * @param farmProduct 农业种植产品
     * @return R
     */
    @Operation(summary = "新增农业种植产品" , description = "新增农业种植产品" )
    @SysLog("新增农业种植产品" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('farm_farmProduct_add')" )
    public R save(@RequestBody FarmProductEntity farmProduct) {
        return R.ok(farmProductService.save(farmProduct));
    }

    /**
     * 修改农业种植产品
     * @param farmProduct 农业种植产品
     * @return R
     */
    @Operation(summary = "修改农业种植产品" , description = "修改农业种植产品" )
    @SysLog("修改农业种植产品" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('farm_farmProduct_edit')" )
    public R updateById(@RequestBody FarmProductEntity farmProduct) {
        return R.ok(farmProductService.updateById(farmProduct));
    }

    /**
     * 通过id删除农业种植产品
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除农业种植产品" , description = "通过id删除农业种植产品" )
    @SysLog("通过id删除农业种植产品" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('farm_farmProduct_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(farmProductService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param farmProduct 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('farm_farmProduct_export')" )
    public List<FarmProductEntity> export(FarmProductEntity farmProduct,Long[] ids) {
        return farmProductService.list(Wrappers.lambdaQuery(farmProduct).in(ArrayUtil.isNotEmpty(ids), FarmProductEntity::getId, ids));
    }
}